package com.minshenglife.zanzan.service.power.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.dao.power.UserPowerDao;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.entity.power.RankPower;
import com.minshenglife.zanzan.pub.CalcUtils;
import com.minshenglife.zanzan.service.power.UserPowerService;

/**
 * 用户权限service
 * 
 * @author wangpeng
 * 
 */
@Component
@Transactional
public class UserPowerServiceImpl implements UserPowerService {
	Logger log = LoggerFactory.getLogger(UserPowerServiceImpl.class);
	@Resource
	private UserPowerDao userPowerDao;

	@Override
	public List<User> findAllUser() {
		List<User> list = userPowerDao.selectAll();
		return list;
	}

	@Override
	public Page<RankPower> findUsesrName(String userName, String dept_name) {
		Page<RankPower> ret = new Page<RankPower>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userName", userName);
		map.put("dept_name", dept_name);
		List<RankPower> rows = userPowerDao.findUserName(map);
		ret.setRows(rows);
		return ret;
	}

	public Page<RankPower> findUserPage(Map<String, Object> map, int pageNo, int pageSize) {
		Page<RankPower> ret = new Page<RankPower>();
		int offset = (pageNo - 1) * pageSize;
		Long count = userPowerDao.countUserAll();
		map.put("from", offset);
		map.put("to", offset + pageSize);
		//System.out.println(offset + "," + (offset + pageSize));
		List<RankPower> list = userPowerDao.findAllUser(map);
		ret.setPageNo(pageNo);
		ret.setPageSize(pageSize);
		ret.setRows(list);
		ret.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		ret.setTotalRow(count);
		return ret;
	}

	@Override
	public User findUserId(String empId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("empId", empId);
		User user = userPowerDao.findUserId(map);
		return user;
	}
}
